package com.controller;

import com.config.Result;
import com.dao.EmployeeDao;
import com.pojo.Employee;
import com.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeServiceImpl employeeService;

    @Autowired
    EmployeeDao employeeDao;

    @GetMapping("/searchAll")
    public Result<?> searchAll(){
        List<Employee> employees = employeeService.FindAllEmployee();
        if (employees == null){
            return Result.error("-1","查看失败");
        }
        return Result.success(employees);
    }

    @PostMapping("/addEmployee")
    public Result<?> addEmployee(@RequestBody Employee employee){
        String username = employee.getUsername();
        String password = employee.getPassword();
        if (username == null && password == null){
            return Result.error("-1","添加失败");
        }

        Employee employee1 = employeeDao.selectByUsername(username);
        if (employee1 != null){
            return Result.error("-1","已存在该账号");
        }

        Integer integer = employeeService.AddEmployee(username, password);
        if (integer != 1){
            return Result.error("-1","添加失败");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteEmployee/{id}")
    public Result<?> deleteDormitory(@PathVariable String id) {
        if (id == null){
            return Result.error("-1","无该工作人员");
        }
        Integer integer = employeeService.DeleteEmployee(id);
        if (integer != 1){
            return Result.error("-1","删除失败");
        }
        return Result.success();
    }

}
